//
//  UPCustomTextFieldCell.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/27.
//

#import "UPCustomTextFieldCell.h"
#import <Masonry/Masonry.h>

@interface UPCustomTextFieldCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *textFieldBgView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIImageView *arrowImgView;
@property (nonatomic, strong) UIButton *arrowButton;

@end

@implementation UPCustomTextFieldCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

#pragma mark - UI Setup
- (void)setupUI {
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textFieldBgView];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.arrowButton];
    [self.contentView addSubview:self.arrowImgView];
}

- (void)setupConstraints {
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(5);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(20);
    }];
    
    [self.textFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(40);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.textFieldBgView);
        make.left.right.equalTo(self.textFieldBgView).inset(7);
    }];
    
    [self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.textFieldBgView);
    }];
    
    [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.textFieldBgView);
        make.right.equalTo(self.textFieldBgView).offset(-10);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(22);
    }];
}

- (void)updateSubviesConstraints {
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        if (!self.arrowImgView.isHidden) {
            make.right.equalTo(self.textFieldBgView).offset(-37);
        }
    }];
}

#pragma mark - Lazy Loading
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor colorWithHexString:@"000000"];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 1;
    }
    return _titleLabel;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:13];
        _textField.textColor = [UIColor colorWithHexString:@"000000"];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

- (UIView *)textFieldBgView {
    if (!_textFieldBgView) {
        _textFieldBgView = [[UIView alloc] init];
        _textFieldBgView.layer.borderColor = [UIColor colorWithHexString:@"000000"].CGColor;
        _textFieldBgView.layer.borderWidth = 1.0;
        _textFieldBgView.layer.masksToBounds = true;
        _textFieldBgView.layer.cornerRadius = 20;
        _textFieldBgView.userInteractionEnabled = YES;
    }
    return _textFieldBgView;
}

- (UIImageView *)arrowImgView {
    if (!_arrowImgView) {
        _arrowImgView = [[UIImageView alloc] init];
        _arrowImgView.contentMode = UIViewContentModeScaleAspectFit;
        _arrowImgView.image = [UIImage imageNamed:@"arrow_forward"];
        _arrowImgView.hidden = YES;
        _arrowImgView.userInteractionEnabled = YES;
    }
    return _arrowImgView;
}

- (UIButton *)arrowButton {
    if (!_arrowButton) {
        _arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_arrowButton addTarget:self action:@selector(clickArrowButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _arrowButton;
}

#pragma mark - Public Methods
- (void)clickArrowButton {
    NSString *type = self.model.admit;
    BOOL isAddress = NO;
    if ([type isEqualToString:@"binfightuous"]) {//enum
        isAddress = NO;
    } else if ([type isEqualToString:@"flaccling"]) {//txt
        isAddress = NO;
    } else if ([type isEqualToString:@"radioeer"]) {//city
        isAddress = YES;
    }
    if (self.arrowButtonBlock) {
        self.arrowButtonBlock(self.model, isAddress);
    }
}

- (void)textChanged:(UITextField *)textField {
//    self.model.propriics = textField.text;
    if (self.textFieldChangedBlock) {
        self.textFieldChangedBlock(self.model, textField.text);
    }
}

- (void)setModel:(PersonalBlackModel *)model {
    _model = model;
    self.titleLabel.text = model.musetic;
    self.textField.placeholder = model.everybodyesque;
    self.textField.text = model.propriics;

    NSString *type = model.admit;
    if ([type isEqualToString:@"binfightuous"]) {//enum
        self.arrowImgView.hidden = NO;
        self.arrowButton.hidden = NO;
    } else if ([type isEqualToString:@"flaccling"]) {//txt
        self.arrowImgView.hidden = YES;
        self.arrowButton.hidden = YES;
        if (model.directorine == 1) {
            self.textField.keyboardType = UIKeyboardTypeNumberPad;
        } else {
            self.textField.keyboardType = UIKeyboardTypeDefault;
        }
    } else if ([type isEqualToString:@"radioeer"]) {//city
        self.arrowImgView.hidden = NO;
        self.arrowButton.hidden = NO;
    }
    [self updateSubviesConstraints];
}

@end
